package com.cf.apiInstance.rest;

import javax.annotation.Resource;

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cf.apiInstance.annotation.ApiLog;
import com.cf.apiInstance.beans.ApiDataRequest;
import com.cf.apiInstance.common.bean.ApiResponse;
import com.cf.apiInstance.common.utils.JsonUtils;
import com.cf.apiInstance.domain.ApiData;
import com.cf.apiInstance.service.ApiDataService;

/**
 * api测试接口
 * 
 * @author: cf
 * @date: 30 Nov 2018 10:01:51 am
 * @vesion: 1.0
 */
@RestController
@RequestMapping("/api/test")
public class ApiDataController {

	@Resource
	private ApiDataService apiDataService;

	@ApiLog(description = "推送一条测试数据")
	@PostMapping(value = "/apiData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ApiResponse<?> putData(@RequestBody String body) throws Exception {
		ApiDataRequest request = JsonUtils.toObject(body, ApiDataRequest.class);
		apiDataService.saveApiData(request.getSubmit());
		return ApiResponse.ok();
	}

	@ApiLog(description = "获取一条api测试数据")
	@GetMapping(value = "/apiData/{id}")
	@ResponseBody
	public ApiResponse<ApiData> gutData(@PathVariable("id") Integer id) throws Exception {
		return ApiResponse.ok(apiDataService.getApiDataById(id));
	}

	@ApiLog(description = "查询测试数据")
	@GetMapping(value = "/apiData", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
	@ResponseBody
	public ApiResponse<ApiData> gutData(@RequestBody String body) throws Exception {
		ApiData queryData = JsonUtils.toObject(body, ApiData.class);
		return ApiResponse.ok(apiDataService.getApiData(queryData));
	}
}
